package com.adtech.adms.commons.web.action;

import java.util.List;

import com.adtech.adms.SessionKey;
import com.adtech.adms.system.pojo.User;
import com.adtech.core.dao.query.FilterItem;
import com.adtech.core.dao.query.FilterMap;
import com.adtech.core.dao.query.Pager;
import com.adtech.core.dao.query.Sort;
import com.adtech.core.exception.ServiceException;
import com.adtech.core.exception.SystemException;
import com.adtech.core.web.action.BaseAction;
import com.opensymphony.xwork2.util.Element;

/**
 * 
 * 分页的主要方法
 * @author 杜凯 
 * @since 2011-3-21 
 * 
 * @Copyright (C) AD Tech Workshop All Right Reserved
 */
public abstract class DataGridAction extends BaseAction {

	private static final long serialVersionUID = -1034462981080635583L;

	@Element(value = FilterItem.class)
	private FilterMap filterMap;
	protected PagingResult resultData;

	/**
	 * @function 执行Ajax查询并将查询结果以json方式返回
	 * @return 跳转结果
	 * @throws ServiceException 
	 * @throws SystemException 
	 * 
	 * @author 杜凯
	 * @history
	 *          Dormin: struts action 里面必须配置此方法名
	 * 
	 */
	public String ajaxLoadData() throws SystemException, ServiceException {
		// 根据查询以及分页参数拼装查询语句执行查询,返回查询结果
		Pager pager = getPager();

		List<?> resultList = getModelDataGridList(pager, getFilterMap(),getSorter());
		resultData = new PagingResult();
		resultData.setTotal(pager.getTotalCount());
		resultData.setRows(resultList);
		resultData.setCurrentPage(pager.getCurrentPage());
		resultData.setTotalPage(pager.getTotalPage());
		return SUCCESS;
	}

	/**
	 * 列表初始页面的转发
	 */
	@Override
	public String execute() {
		return SUCCESS;
	}

	/**
	 * @function 推迟到子类的过去数据集合的具体方法
	 * @param pager
	 *            分页参数
	 * @param filtermap
	 *            查询条件
	 * @param sort
	 *            排序方式
	 * 
	 * @return 返回查询结果列表
	 * @throws ServiceException 
	 * @throws SystemException 
	 * 
	 * @author 杜凯
	 * @history
	 * 
	 */
	protected abstract List<?> getModelDataGridList(Pager pager, FilterMap filtermap, Sort sort) 
	throws SystemException, ServiceException;

	public PagingResult getResultData() {
		return resultData;
	}
	
	public FilterMap getFilterMap() {
		return filterMap;
	}

	public void setFilterMap(FilterMap filterMap) {
		this.filterMap = filterMap;
	}

	/**
	 * @function 获取当前登录用户
	 * @return 当前登录用户信息
	 * @author Dormin
	 */
	public User getUser(){
		return (User) session.get(SessionKey.SESSION_KEY_USER);
	}
}
